﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IndoReputation.Models.BusinessObject;
using System.Data.SqlClient;
using System.Data;
using IndoReputation.Models.BusinessObject.Collections;

namespace IndoReputation.Models.DataAccess
{
    public class OfficeTypeDAL
    {
        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  9:39 AM
        /// Todo: Insert a Object User to Database
        /// </summary>
        /// <param name="officeType">A officeType.</param>
        /// <returns></returns>
        public static int Insert(OfficeType officeType)
        {
            try
            {
                string spName = "OfficeType_Insert";
                SqlParameter[] spPara = new SqlParameter[7];
                spPara[0] = new SqlParameter("@ID", SqlDbType.Int, 2, ParameterDirection.Output, false, 0, 0, null, DataRowVersion.Default, null);
                spPara[1] = new SqlParameter("@Name", officeType.Name);
                spPara[2] = new SqlParameter("@Description", officeType.Description);
                spPara[3] = new SqlParameter("@CreateAt", officeType.CreateAt);
                spPara[4] = new SqlParameter("@CreateBy", officeType.CreateBy);
                spPara[5] = new SqlParameter("@UpdateAt", DBNull.Value);
                spPara[6] = new SqlParameter("@UpdateBy", DBNull.Value);                
                DataAccessBase.ExecuteNonQuery(CommandType.StoredProcedure, spName, spPara);
                return Convert.ToInt32(spPara[0].Value);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  9:45 AM
        /// Todo: Update a User Exists in Database 
        /// </summary>
        /// <param name="officeType">A officeType.</param>
        /// <returns></returns>
        public static bool Update(OfficeType officeType)
        {
            try
            {
                string spName = "OfficeType_UpdateByPK";
                SqlParameter[] spPara = new SqlParameter[7];
                spPara[0] = new SqlParameter("@ID", officeType.ID);
                spPara[1] = new SqlParameter("@Name", officeType.Name);
                spPara[2] = new SqlParameter("@Description", officeType.Description);
                spPara[3] = new SqlParameter("@CreateAt", officeType.CreateAt);
                spPara[4] = new SqlParameter("@CreateBy", officeType.CreateBy);
                spPara[5] = new SqlParameter("@UpdateAt", officeType.UpdateAt);
                spPara[6] = new SqlParameter("@UpdateBy", officeType.UpdateBy);
                int totalRecordEffected = 0;
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, spPara);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                totalRecordEffected = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                            }
                        }
                    }
                }
                if (totalRecordEffected > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception Ex)
            {
                return false;
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:36 AM
        /// Todo: Delete a User Existst in Database
        /// </summary>
        /// <param name="user">A user.</param>
        /// <returns></returns>
        public static bool Delete(int officeTypeID)
        {
            try
            {
                string spName = "OfficeType_DeleteByPK";
                SqlParameter[] spPara = new SqlParameter[1];
                spPara[0] = new SqlParameter("@ID", officeTypeID);
                int totalRecordEffected = 0;
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, spPara);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                totalRecordEffected = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                            }
                        }
                    }
                }
                if (totalRecordEffected > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception Ex)
            {
                return false;
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/29/2011  5:23 PM
        /// Todo: Get All Users
        /// </summary>
        /// <returns></returns>
        public static OfficeTypeList GetAllOfficeType()
        {
            try
            {
                OfficeTypeList listOfficeType = null;
                string spName = "OfficeType_SelectAll";
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, null);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                listOfficeType = new OfficeTypeList();
                                List<OfficeType> listTemp = DataAccessBase.ConvertToList<OfficeType>(ds.Tables[0]);
                                foreach (OfficeType _r in listTemp)
                                    listOfficeType.Add(_r);
                            }
                        }
                    }
                }
                return listOfficeType;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Author: Nguyen Duc Thanh Tung
        /// Created on: 11/30/2011  10:40 AM
        /// Todo: Select a User existst in Database
        /// </summary>
        /// <param name="officeTypeID">The user ID.</param>
        /// <returns></returns>
        public static OfficeType GetOfficeTypeById(int officeTypeID)
        {
            try
            {
                OfficeType officeType = null;
                string spName = "OfficeType_SelectByPK";
                SqlParameter[] spPara = new SqlParameter[1];
                spPara[0] = new SqlParameter("@ID", officeTypeID);
                DataSet ds = DataAccessBase.ExecuteDataset(CommandType.StoredProcedure, spName, spPara);
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0] != null)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                officeType = DataAccessBase.ConvertToEntity<OfficeType>(ds.Tables[0].Rows[0]);
                            }
                        }
                    }
                }
                return officeType;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
    }
}